package org.robert.export_client.excel.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.CellStyle;
import org.robert.export_client.excel.metadata.ExcelHeadEntity;
import org.robert.export_client.excel.util.ExcelUtil;

import java.util.List;

/**
 * @Description
 * @Author robert
 * @Date 2024/1/20 21:57
 **/
public class ExcelHeadService {

    public static void doHead(List<ExcelHeadEntity> headEntityList, HSSFSheet sheet,
                              CellStyle titleStyle, int rowHeight) {
        if(CollectionUtils.isEmpty(headEntityList) || sheet == null){
            return;
        }
        // 获取字段名
        HSSFRow row = ExcelUtil.createRow(sheet, 0, rowHeight == 0 ? 350 : rowHeight);//第一行
        for (int j=0;j<headEntityList.size();j++){
            HSSFCell cell = ExcelUtil.createCell(row, j, null);
            ExcelUtil.setColumnWidth(sheet, j, headEntityList.get(j).getColumnWidth());
            cell.setCellValue(headEntityList.get(j).getColumnName());
            cell.setCellStyle(titleStyle); // 应用样式到单元格
        }
    }


    public static void doHead(List<ExcelHeadEntity> headEntityList, HSSFSheet sheet, CellStyle titleStyle) {
        doHead(headEntityList, sheet, titleStyle, 350);
    }

}
